//
//  PPAddFriendViewController.m
//  PetParadise
//
//  Created by 陈无忌 on 8/19/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPAddFriendViewController.h"
#import "supportingHeadFiles.h"
#import "PPUserInfo.h"
#import "PPOtherUserCardViewController.h"
#import "PPZBarViewController.h"
#import "SBJson.h"

@interface PPAddFriendViewController ()<UITextFieldDelegate, ScanQRCodeDelegate>
@property (weak, nonatomic) IBOutlet UITextField *searchTextField;

@end

@implementation PPAddFriendViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.scrollEnabled = NO;
    self.searchTextField.delegate = self;

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 44;
    }else{
        return 10;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view;
    if (section == 0) {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 18, 200, 16)];
        label.text = @"搜索号码添加好友";
        [view addSubview:label];
    }else{
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
    }
    return view;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([textField isEqual:self.searchTextField]) {
        NSString *url = [PPAPIList getAPIList].PPQueryFriendByPhoneNum;
        NSDictionary *params =@{@"phoneNum":self.searchTextField.text};
        [httpRequst POST:url parameters:params success:^(AFHTTPRequestOperation *operation,id responseObject){
            NSLog(@"%@",responseObject);
            NSNumber *processResult = responseObject[@"processResult"];
            NSString *msg = responseObject[@"errorMsg"];
            if (processResult.intValue == 1) {
                NSError *err = nil;
                NSDictionary *resultMap = responseObject[@"resultMap"];
                NSDictionary *friendDic = resultMap[@"friend"];
                PPUserInfo *user = [[PPUserInfo alloc] initWithDictionary:friendDic error:&err];
                PPOtherUserCardViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PPOtherUserCardViewController"];
                targetVC.user = user;
                [self.navigationController pushViewController:targetVC animated:YES];
            }else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [alert show];
            }
            
            
        }failure:^(AFHTTPRequestOperation *operation, NSError *error){
            
        }];
    }
    return YES;
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"AddFriendToScanVC"]) {
        PPZBarViewController *targetVC = segue.destinationViewController;
        targetVC.delegate = self;
    }
}



#pragma mark - Scan Delegate

- (void)scanFinishedWithResult:(NSString *)resultString
{
    PPOtherUserCardViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PPOtherUserCardViewController"];
    NSDictionary *resultDic = [resultString JSONValue];
    NSNumber *friendID = resultDic[@"userid"];
    if (friendID) {
        
        NSString *url = [PPAPIList getAPIList].PPQueryUserMsgByUserId;
        NSDictionary *params =@{@"userId":@(friendID.intValue)};
        [httpRequst POST:url parameters:params success:^(AFHTTPRequestOperation *operation,id responseObject){
//            NSLog(@"%@",responseObject);
            NSNumber *processResult = responseObject[@"processResult"];
            if (processResult.intValue == 1) {
                NSError *err = nil;
                NSDictionary *resultMap = responseObject[@"resultMap"];
                NSDictionary *userDic = resultMap[@"user"];
                PPUserInfo *user = [[PPUserInfo alloc] initWithDictionary:userDic error:&err];
                targetVC.user = user;
                [self.navigationController pushViewController:targetVC animated:YES];
            }else{
                [UIAlertView showWithTitle:@"无效二维码" message:@"没有该用户" cancelButtonTitle:@"确定" otherButtonTitles:nil tapBlock:nil];
            }
        }failure:^(AFHTTPRequestOperation *operation, NSError *error){
            NSLog(@"%@",error);
            [UIAlertView showWithTitle:@"提示" message:@"网络不给力" cancelButtonTitle:@"确定" otherButtonTitles:nil tapBlock:nil];
        }];
    }else {
        [UIAlertView showWithTitle:@"无效二维码" message:@"请确保您所扫的二维码为有效宠缘二维码" cancelButtonTitle:@"确定" otherButtonTitles:nil tapBlock:nil];
    }
}


@end
